package dao.impl;

import bean.UserLogin;
import dao.UserInfoDao;
import util.DbUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class UserInfoDaoImpl extends DbUtil implements UserInfoDao {

    private List<UserLogin> userLogins;

    public UserInfoDaoImpl() {
    }

    public UserInfoDaoImpl(List<UserLogin> userLogins) {
        this.userLogins = userLogins;
    }

    @Override
    public String toString() {
        return "UserInfoDaoImpl{" +
                "userLogins=" + userLogins +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserInfoDaoImpl that = (UserInfoDaoImpl) o;
        return Objects.equals(userLogins, that.userLogins);
    }

    @Override
    public int hashCode() {
        return Objects.hash(userLogins);
    }

    public List<UserLogin> getUserLogins() {
        return userLogins;
    }

    public void setUserLogins(List<UserLogin> userLogins) {
        this.userLogins = userLogins;
    }

    @Override
    public List<UserLogin> allInfo() {
        List<UserLogin> userLogins = new ArrayList<>();

        try {
            String sql = "select * from userlogin";

            ResultSet rs = query(sql);

            while (rs.next()){
                //对象AirInfo为局部变量，每次循环重新创建
                UserLogin userLogin = new UserLogin();
                userLogin.setUserid(rs.getInt("userid"));
                userLogin.setUserName(rs.getString("username"));
                userLogin.setPassWord(rs.getString("password"));
                userLogins.add(userLogin);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll();
        }


        return userLogins;
    }


}
